{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第十五讲：子空间投影\n",
    "\n",
    "从$\\mathbb{R}^2$空间讲起，有向量$a, b$，做$b$在$a$上的投影$p$，如图：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW8AAAD7CAYAAAClvBX1AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl4VPXZPvD7mSRgWEIgvgRkS3KB5VXQCEiRNWwCBREK\nxfoCElAsosUKohKrBGsVW7hARbvJpgUFXFBpEOgPBm1f0ADyFlFACQkuLJWGNQhJ5vn9MZmQPbN/\n55zcn+uaS2fJOXdOp4/f3Dk5I6oKIiKyFofpAERE5DsObyIiC+LwJiKyIA5vIiIL4vAmIrIgDm8i\nIguKDteORITnJBIR+UFVpeJjYV15q2rIbnPnzg3p9kN9Y35mZ35r3kKdvzq2qU1yc3NNRwgI85tj\n5ewA85tmKr9thjcRUV1im+Gdnp5uOkJAmN8cK2cHmN80U/mlpk4lqDsS0XDti4jILkQEavoXlqHk\ndDpNRwgI85tj5ewA85tmKr9thjcRUV3C2oSIKILZvjYhIqpLbDO82ZuZZeX8Vs4OML9plu+8RcQh\nIntE5L1gbZOIiKoWtM5bRB4C0BVAnKqOrOJ5dt5ERD4KaectIq0B/ATAK8HYHhER1SxYtckiALMB\nGFtaszczy8r5rZwdYH7TLNt5i8hwACdUdS8AKbkRURg89Ow+zFzzQo1XnyN7CrjzFpFnAEwAUAQg\nFkBjAG+r6l0VXqeTJk1CUlISACA+Ph6pqalIS0sDcOW/XrzP+7zv3f033wRe+sdRYPQkzG07F2nJ\naRGVj/f9u+90OrFixQoAQFJSEubNm1dl5x3UP9IRkX4AZvEXlkShtXIlcN99QGHqy3ANmYGmsfHI\n+1UeGtZraDoaBZnt/0jH818uq2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SH/46r6fslr\nHgdQqKqrDUSsk0SkEYA3ATxYsgKPeCIyHMAJVd1bUndG3Pu9FtEAugC4X1V3ichiAI8BmBvOAJai\nqoOre05EpgF4u+R12SW/+EtQ1VNhC1iD6rKLSCcASQD+T0QE7spht4h0V9WTYYxYo5qOPQCISDrc\nPwYPCEugwH0LoG2Z+61LHrMMEYmGe3C/pqrvms7jg14ARorITwDEAmgsIq+q6l2Gc3nrG7h/Ut5V\ncv9NAGH9hbfdapP1KBkcInItgJhIGdw1UdXPVLWFqqaoajLcb4ybImlw10ZEhsL9I/BIVb1kOo+X\nsgG0F5F2IlIPwM8BWO2sh2UAPlfV500H8YWqZqhqW1VNgfu4b7XQ4IaqngDwdcmcAYCBCPMvXi23\n8q7FcgDLRGQfgEsALPNmqEBhvR8jXwRQD8AW9w8P2Kmq081GqpmqFovIA3CfKeMAsFRVw3rGQCBE\npBeA8QD2icincL9vMlT1A7PJ6owZAFaJSAyAHACTw7lz/nk8EZEF2a02ISKqEzi8iYgsiMObiMiC\nOLyJiCyIw5uIyII4vImILIjDm4jIgji8iYgs6P8DKEoXb/fywMMAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1044a9b38>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "plt.style.use(\"seaborn-dark-palette\")\n",
    "\n",
    "fig = plt.figure()\n",
    "plt.axis('equal')\n",
    "plt.axis([-7, 7, -6, 6])\n",
    "plt.arrow(-4, -1, 8, 2, head_width=0.3, head_length=0.5, color='r', length_includes_head=True)\n",
    "plt.arrow(0, 0, 2, 4, head_width=0.3, head_length=0.5, color='b', length_includes_head=True)\n",
    "plt.arrow(0, 0, 48/17, 12/17, head_width=0.3, head_length=0.5, color='gray', length_includes_head=True)\n",
    "plt.arrow(48/17, 12/17, 2-48/17, 4-12/17, head_width=0.3, head_length=0.5, color='g', length_includes_head=True)\n",
    "# plt.plot([48/17], [12/17], 'o')\n",
    "# y=1/4x\n",
    "# y=-4x+12\n",
    "# x=48/17\n",
    "# y=12/17\n",
    "plt.annotate('b', xy=(1, 2), xytext=(-30, 15), textcoords='offset points', size=20, arrowprops=dict(arrowstyle=\"->\"))\n",
    "plt.annotate('a', xy=(-1, -0.25), xytext=(15, -30), textcoords='offset points', size=20, arrowprops=dict(arrowstyle=\"->\"))\n",
    "plt.annotate('e=b-p', xy=(2.5, 2), xytext=(30, 0), textcoords='offset points', size=20, arrowprops=dict(arrowstyle=\"->\"))\n",
    "plt.annotate('p=xa', xy=(2, 0.5), xytext=(-20, -40), textcoords='offset points', size=20, arrowprops=dict(arrowstyle=\"->\"))\n",
    "plt.grid()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "plt.close(fig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从图中我们知道，向量$e$就像是向量$b, p$之间的误差，$e=b-p, e \\bot p$。$p$在$a$上，有$\\underline{p=ax}$。\n",
    "\n",
    "所以有$a^Te=a^T(b-p)=a^T(b-ax)=0$。关于正交的最重要的方程：\n",
    "\n",
    "$$\n",
    "a^T(b-xa)=0 \\\\\n",
    "\\underline{xa^Ta=a^Tb} \\\\\n",
    "\\underline{x=\\frac{a^Tb}{a^Ta}} \\\\\n",
    "p=a\\frac{a^Tb}{a^Ta}\n",
    "$$\n",
    "\n",
    "从上面的式子可以看出，如果将$b$变为$2b$则$p$也会翻倍，如果将$a$变为$2a$则$p$不变。\n",
    "\n",
    "设投影矩阵为$P$，则可以说投影矩阵作用与某个向量后，得到其投影向量，$projection_p=Pb$。\n",
    "\n",
    "易看出$\\underline{P=\\frac{aa^T}{a^Ta}}$，若$a$是$n$维列向量，则$P$是一个$n \\times n$矩阵。\n",
    "\n",
    "观察投影矩阵$P$的列空间，$C(P)$是一条通过$a$的直线，而$rank(P)=1$（一列乘以一行：$aa^T$，而这一列向量$a$是该矩阵的基）。\n",
    "\n",
    "投影矩阵的性质：\n",
    "\n",
    "* $\\underline{P=P^T}$，投影矩阵是一个对称矩阵。\n",
    "* 如果对一个向量做两次投影，即$PPb$，则其结果仍然与$Pb$相同，也就是$\\underline{P^2=P}$。\n",
    "\n",
    "为什么我们需要投影？因为就像上一讲中提到的，有些时候$Ax=b$无解，我们只能求出最接近的那个解。\n",
    "\n",
    "$Ax$总是在$A$的列空间中，而$b$却不一定，这是问题所在，所以我们可以将$b$变为$A$的列空间中最接近的那个向量，即将无解的$Ax=b$变为求有解的$A\\hat{x}=p$（$p$是$b$在$A$的列空间中的投影，$\\hat{x}$不再是那个不存在的$x$，而是最接近的解）。\n",
    "\n",
    "现在来看$\\mathbb{R}^3$中的情形，将向量$b$投影在平面$A$上。同样的，$p$是向量$b$在平面$A$上的投影，$e$是垂直于平面$A$的向量，即$b$在平面$A$法方向的分量。\n",
    "设平面$A$的一组基为$a_1, a_2$，则投影向量$p=\\hat{x_1}a_1+\\hat{x_2}a_2$，我们更倾向于写作$p=A\\hat{x}$，这里如果我们求出$\\hat{x}$，则该解就是无解方程组最近似的解。\n",
    "\n",
    "现在问题的关键在于找$e=b-A\\hat{x}$，使它垂直于平面，因此我们得到两个方程\n",
    "$\n",
    "\\begin{cases}a_1^T(b-A\\hat{x})=0\\\\\n",
    "a_2^T(b-A\\hat{x})=0\\end{cases}\n",
    "$，将方程组写成矩阵形式\n",
    "$\n",
    "\\begin{bmatrix}a_1^T\\\\a_2^T\\end{bmatrix}\n",
    "(b-A\\hat{x})=\n",
    "\\begin{bmatrix}0\\\\0\\end{bmatrix}\n",
    "$，即$A^T(b-A\\hat{x})=0$。\n",
    "\n",
    "比较该方程与$\\mathbb{R}^2$中的投影方程，发现只是向量$a$变为矩阵$A$而已，本质上就是$A^Te=0$。所以，$e$在$A^T$的零空间中（$e\\in N(A^T)$），从前面几讲我们知道，左零空间$\\bot$列空间，则有$e\\bot C(A)$，与我们设想的一致。\n",
    "\n",
    "再化简方程得$A^TAx=A^Tb$，比较在$\\mathbb{R}^2$中的情形，$a^Ta$是一个数字而$A^TA$是一个$n$阶方阵，而解出的$x$可以看做两个数字的比值。现在在$\\mathbb{R}^3$中，我们需要再次考虑：什么是$\\hat{x}$？投影是什么？投影矩阵又是什么？\n",
    "\n",
    "* 第一个问题：$\\hat x=(A^TA)^{-1}A^Tb$；\n",
    "* 第二个问题：$p=A\\hat x=\\underline{A(A^TA)^{-1}A^T}b$，回忆在$\\mathbb{R}^2$中的情形，下划线部分就是原来的$\\frac{aa^T}{a^Ta}$；\n",
    "* 第三个问题：易看出投影矩阵就是下划线部分$P=A(A^TA)^{-1}A^T$。\n",
    "\n",
    "这里还需要注意一个问题，$P=A(A^TA)^{-1}A^T$是不能继续化简为$P=AA^{-1}(A^T)^{-1}A^T=I$的，因为这里的$A$并不是一个可逆方阵。\n",
    "也可以换一种思路，如果$A$是一个$n$阶可逆方阵，则$A$的列空间是整个$\\mathbb{R}^n$空间，于是$b$在$\\mathbb{R}^n$上的投影矩阵确实变为了$I$，因为$b$已经在空间中了，其投影不再改变。\n",
    "\n",
    "再来看投影矩阵$P$的性质：\n",
    "* $P=P^T$：有\n",
    "$\n",
    "\\left[A(A^TA)^{-1}A^T\\right]^T=A\\left[(A^TA)^{-1}\\right]^TA^T\n",
    "$，而$(A^TA)$是对称的，所以其逆也是对称的，所以有$A((A^TA)^{-1})^TA^T=A(A^TA)^{-1}A^T$，得证。\n",
    "* $P^2=P$：有\n",
    "$\n",
    "\\left[A(A^TA)^{-1}A^T\\right]\\left[A(A^TA)^{-1}A^T\\right]=A(A^TA)^{-1}\\left[(A^TA)(A^TA)^{-1}\\right]A^T=A(A^TA)^{-1}A^T\n",
    "$，得证。\n",
    "\n",
    "## 最小二乘法\n",
    "\n",
    "接下看看投影的经典应用案例：最小二乘法拟合直线（least squares fitting by a line）。\n",
    "\n",
    "我们需要找到距离图中三个点 $(1, 1), (2, 2), (3, 2)$ 偏差最小的直线：$b=C+Dt$。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD7CAYAAABpJS8eAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHglJREFUeJzt3Xt0VPX57/H3ExSJCEKLcgnFAPXW2hIuIpTTZY63KPAT\nta5qwcWKXRy8IMqxP9HWUrwutNUWRWllHSpSfhU1p8fSUlBbCRCk2h8alRAESUIFKoqAFI0Y4Tl/\nZMjOwITMMGP2TubzWmuWs/f+Zu9vHsmTnc/s2WPujoiItH05YU9ARERahhq+iEiWUMMXEckSavgi\nIllCDV9EJEuo4YuIZIljwp7AocxM14mKiKTI3a25MZE8w3f3rH+oFsFj+vTpoc8hKg/VQrVI9EhW\nJBu+SGM1NTVhTyEyVIuAapE6NXwRkSyhhi+RV1xcHPYUIkO1CKgWqbNU8p+WYGYetTmFwaz+9RfV\nQkSaY2Z4S7xoa2a9zexlM6sws7fN7OYEY841s91m9nrs8dN0jyvZo7S0NOwpRIZqEVAtUpeJyzK/\nAG5193IzOwFYY2Yvuvv6Q8atcPdLM3A8ERE5ChmPdMzseWCWu/+t0bpzgf909/9I4usV6aBIR0SS\n12KRziEHzQcKgFcTbB5uZuVmttjMvpHJ44qISPMy1vBjcU4JcIu77z1k8xqgj7sXAI8Bz2fquNL2\nKasNqBYB1SJ1Gbm1gpkdQ32z/527//HQ7Y1/Abj7EjObbWZfcfedifZXXFxMfn4+AF26dKGgoIDC\nwkIg+J/c1pcPisp8wlwuLy+P1HzCXC4vL4/UfLQczvLB56m++SwjGb6ZzQd2uPutTWzv7u7bY8+H\nAs+6e34TY5XhowxfRJKXbIaf9hm+mY0AxgFvm9kbgAM/AU4B3N3nAFea2Q1AHVALXJXucUVEJDVp\nZ/juvsrd27l7gbsPdPdB7r7U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Emr4EnmpvpuwLVMtAqpF6iJ5WWbYcxARaW2SuSwz\ncg1fRES+HIp0RESyhBq+iEiWiFzDN7MrzWytme03s0FhzycMZnaxma03sw2xu5BmJTObG7sF91th\nzyVsZtbbzF42swoze9vMbg57TmExs+PM7FUzeyNWi+lhzylsZpZjZq+b2aIjjYtcwwfeBi4Hloc9\nkTCYWQ7wGFAEfBP4gZmdEe6sQvMk9XUQ+AK41d2/CQwHJmXrvwt33wf8T3cfSP2NGS8xs6EhTyts\ntwDrmhsUuYbv7u+4+0bqP1krGw0FNrr7ZnevAxZS/9nBWSf2qWjNfvB9NnD39929PPZ8L1AJ5IU7\nq/C4+6exp8dRf9ffrL36xMx6AyOB/9Pc2Mg1fCEPeK/R8hay+AdbDmdm+dSf2b4a7kzCE4sw3gDe\nB15y93+EPacQ/Qq4jSR+6YXS8M3sJTN7q9Hj7dh//yOM+Yi0FmZ2AlAC3BI7089K7n4gFun0Bs4x\ns2+EPacwmNkoYHvsrz+jmWQkrQ9AOVpH+pxcYSvQp9Fy79g6yXJmdgz1zf537v7HsOcTBe6+x8yW\nAReTRIbdBo0ALjWzkUAu0MnM5rv7+ESDox7pZGOO/w/g62Z2ipm1B64GjvjKexvX7FlLFvktsM7d\nHwl7ImEys25mdmLseS5wIbA+3FmFw91/4u593L0f9b3i5aaaPUSw4ZvZZWb2HjAM+LOZLQl7Ti3J\n3fcDN1H/Ye8VwEJ3rwx3VuEws98DrwCnmdk/zezasOcUFjMbAYwDzotdjvi6mV0c9rxC0hNYZmbl\n1L+O8YK7/yXkObUKurWCiEiWiNwZvoiIfDnU8EVEsoQavohIllDDFxHJEmr4IiJZQg1fRCRLqOGL\niGQJNXwRkSzx/wF1KKmlk+PdHwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1175bd2b0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.style.use(\"seaborn-dark-palette\")\n",
    "\n",
    "fig = plt.figure()\n",
    "plt.axis('equal')\n",
    "plt.axis([-1, 4, -1, 3])\n",
    "plt.axhline(y=0, c='black', lw='2')\n",
    "plt.axvline(x=0, c='black', lw='2')\n",
    "\n",
    "plt.plot(1, 1, 'o', c='r')\n",
    "plt.plot(2, 2, 'o', c='r')\n",
    "plt.plot(3, 2, 'o', c='r')\n",
    "\n",
    "plt.annotate('(1, 1)', xy=(1, 1), xytext=(-40, 20), textcoords='offset points', size=14, arrowprops=dict(arrowstyle=\"->\"))\n",
    "plt.annotate('(2, 2)', xy=(2, 2), xytext=(-60, -5), textcoords='offset points', size=14, arrowprops=dict(arrowstyle=\"->\"))\n",
    "plt.annotate('(3, 2)', xy=(3, 2), xytext=(-18, 20), textcoords='offset points', size=14, arrowprops=dict(arrowstyle=\"->\"))\n",
    "\n",
    "plt.grid()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "plt.close(fig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "根据条件可以得到方程组 \n",
    "$\n",
    "\\begin{cases}\n",
    "C+D&=1 \\\\\n",
    "C+2D&=2 \\\\\n",
    "C+3D&=2 \\\\\n",
    "\\end{cases}\n",
    "$，写作矩阵形式\n",
    "$\\begin{bmatrix}1&1 \\\\1&2 \\\\1&3\\\\\\end{bmatrix}\\begin{bmatrix}C\\\\D\\\\\\end{bmatrix}=\\begin{bmatrix}1\\\\2\\\\2\\\\\\end{bmatrix}$，也就是我们的$Ax=b$，很明显方程组无解。但是$A^TA\\hat x=A^Tb$有解，于是我们将原是两边同时乘以$A^T$后得到的新方程组是有解的，$A^TA\\hat x=A^Tb$也是最小二乘法的核心方程。\n",
    "\n",
    "下一讲将进行最小二乘法的验算。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
